/*
 * @Author: wangmyng wangmyng@163.com
 * @Date: 2025-10-30 22:41:22
 * @LastEditors: wangmyng wangmyng@163.com
 * @LastEditTime: 2025-11-01 22:26:40
 */
import Map from "@geoscene/core/Map.js";
import MapView from "@geoscene/core/views/MapView.js";
import { set } from "lodash";

import "./index.less";
import MilitaryEditTool from "@/utils/gis/MilitaryEditTool";
import MixGraphicsLayer from "@/utils/gis/MixGraphicsLayer";
import MixEditTool from "@/utils/gis/MixEditTool";

export default function useMap2D() {
  const [map2D, setMap2D] = useState<Map | null>(null);
  const [view2D, setView2D] = useState<MapView | null>(null);
  const [isLoaded2D, setIsLoaded2D] = useState<boolean>(false);
  const [MilitaryEditToolInstance, setMilitaryEditToolInstance] =
    useState<MilitaryEditTool | null>(null);
  const [MixGraphicsLayerInstance, setMixGraphicsLayerInstance] =
    useState<MixGraphicsLayer | null>(null);
  const [MixEditToolInstance, setMixEditToolInstance] =
    useState<MixEditTool | null>(null);

  useEffect(() => {
    initMap();
  }, []);

  const initMap = () => {
    const {
      map2D,
      view2D,
      MilitaryEditToolInstance,
      MixGraphicsLayerInstance,
      MixEditToolInstance,
    } = window?.map || {};
    if (map2D && view2D) {
      setMap2D(map2D);
      setView2D(view2D);
      setMilitaryEditToolInstance(MilitaryEditToolInstance);
      setMixGraphicsLayerInstance(MixGraphicsLayerInstance);
      setMixEditToolInstance(MixEditToolInstance);
      setIsLoaded2D(true);
    } else {
      initializeMapWithSecureLayers();
    }
  };

  // 初始化地图和加载安全图层的函数w
  function initializeMapWithSecureLayers() {
    console.log("initializeMapWithSecureLayers");

    const map = new Map({
      basemap: "tianditu-image",
    });

    const view = new MapView({
      container: "gis-viewer",
      map: map,
      center: [114.92757617235301, 22.739862039229976],
      zoom: 13,
    });

    const mEditTool = new MilitaryEditTool({
      config: {
        activeView: view,
      },
    });

    const mixEditTool = new MixEditTool({
      config: {
        activeView: view,
      },
    });

    const mixGraphicsLayer = new MixGraphicsLayer({ id: "mixGraphicsLayer" });
    map.add(mixGraphicsLayer);

    // 将地图对象和视图对象存储在全局变量中
    set(window, "map", {
      ...(window?.map || {}),
      map2D: map,
      view2D: view,
      MilitaryEditToolInstance: mEditTool,
      MixGraphicsLayerInstance: mixGraphicsLayer,
      MixEditToolInstance: mixEditTool,
    });

    view.when(() => {
      setIsLoaded2D(true);
      setMap2D(map);
      setView2D(view);
      setMilitaryEditToolInstance(mEditTool);
      setMixGraphicsLayerInstance(mixGraphicsLayer);
      setMixEditToolInstance(mixEditTool);
      // 将地图对象和视图对象存储在全局变量中
      set(window, "map", {
        ...(window?.map || {}),
        isLoaded2D: true,
        MilitaryEditToolInstance: mEditTool,
        MixGraphicsLayerInstance: mixGraphicsLayer,
        MixEditToolInstance: mixEditTool,
      });
    });
  }

  const MapViewport2D = useMemo(() => {
    return <div id="gis-viewer"></div>;
  }, []);

  return {
    map2D,
    view2D,
    MilitaryEditToolInstance,
    MixGraphicsLayerInstance,
    MixEditToolInstance,
    isLoaded2D,
    MapViewport2D,
  };
}
